package jsqlite;

import kotlin.UByte;

/* loaded from: classes2.dex */
public class StringEncoder {
    static final char[] xdigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static String encode(byte[] bArr) {
        int i;
        if (bArr == null || bArr.length == 0) {
            return "x";
        }
        int[] iArr = new int[256];
        for (byte b : bArr) {
            int i2 = b & UByte.MAX_VALUE;
            iArr[i2] = iArr[i2] + 1;
        }
        int length = bArr.length;
        byte b2 = 1;
        for (int i3 = 1; i3 < 256; i3++) {
            if (i3 != 39 && (i = iArr[i3] + iArr[(i3 + 1) & 255] + iArr[(i3 + 39) & 255]) < length) {
                b2 = i3;
                length = i;
                if (i == 0) {
                    break;
                }
            }
        }
        StringBuffer stringBuffer = new StringBuffer(bArr.length + length + 1);
        stringBuffer.append((char) b2);
        for (byte b3 : bArr) {
            char c = (char) ((b3 - b2) & 255);
            if (c == 0) {
                stringBuffer.append((char) 1);
                stringBuffer.append((char) 1);
            } else if (c == 1) {
                stringBuffer.append((char) 1);
                stringBuffer.append((char) 2);
            } else if (c == '\'') {
                stringBuffer.append((char) 1);
                stringBuffer.append((char) 3);
            } else {
                stringBuffer.append(c);
            }
        }
        return stringBuffer.toString();
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Removed duplicated region for block: B:19:0x004f  */
    /* JADX WARN: Removed duplicated region for block: B:20:0x0052  */
    /* JADX WARN: Removed duplicated region for block: B:21:0x0055  */
    /* JADX WARN: Removed duplicated region for block: B:22:0x0058  */
    /* JADX WARN: Removed duplicated region for block: B:23:0x005b  */
    /* JADX WARN: Removed duplicated region for block: B:24:0x005e  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x0075  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x0089  */
    /* JADX WARN: Removed duplicated region for block: B:41:0x008c  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x008f  */
    /* JADX WARN: Removed duplicated region for block: B:43:0x0092  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x0095  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public static byte[] decode(java.lang.String r21) {
        /*
            Method dump skipped, instructions count: 384
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: jsqlite.StringEncoder.decode(java.lang.String):byte[]");
    }

    private static byte[] byteCopy(byte[] bArr, int i, int i2, byte[] bArr2) {
        int i3 = 0;
        int i4 = i;
        while (i4 < i + i2) {
            bArr2[i3] = bArr[i4];
            i4++;
            i3++;
        }
        return bArr2;
    }

    public static String encodeX(byte[] bArr) {
        if (bArr == null || bArr.length == 0) {
            return "X''";
        }
        int i = 2;
        char[] cArr = new char[(bArr.length * 2) + 3];
        for (int i2 = 0; i2 < bArr.length; i2++) {
            int i3 = i + 1;
            char[] cArr2 = xdigits;
            cArr[i] = cArr2[(bArr[i2] >> 4) & 15];
            i = i3 + 1;
            cArr[i3] = cArr2[bArr[i2] & 15];
        }
        cArr[0] = 'X';
        cArr[1] = '\'';
        cArr[i] = '\'';
        return new String(cArr);
    }
}
